package com.lyf.viewapp.nfc;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.nfc.Tag;
import android.nfc.tech.NfcV;
import android.util.Log;

import com.example.hf15693.NfcV.NfcVClassCard;
import com.example.hf15693.NfcV.NfcVUtil;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by lyf on 2018/8/30 12:58
 *
 * @author lyf
 * desc：NFC工具类
 */
public class NfcUtil {

    public static final String NFC_ACTION_OTHER = "android.nfc.action.TECH_DISCOVERED";

    /**
     * nfc读取标签方法（凯路威）
     * @param intent 意图
     * @param startPoint 起始块数
     * @param count 数量
     * @return NfcBean对象
     */
    @SuppressLint("SimpleDateFormat")
    public static NfcBean resolveIntent(Intent intent, int startPoint, int count) {

        if (!NFC_ACTION_OTHER.equals(intent.getAction())) {
            return null;
        }
        NfcV mNfcV = NfcV.get((Tag) intent
                .getParcelableExtra("android.nfc.extra.TAG"));
        NfcVClassCard nfcVCard = null;
        try {
            NfcBean nfcBean = new NfcBean();
            mNfcV.connect();
            NfcVUtil mNfcVUtil = new NfcVUtil(mNfcV);
            String uid = mNfcVUtil.Inventory().toUpperCase();
            nfcBean.setUid(uid);
            Log.e("nfc", "UID:" + uid);

            SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
            String startTime = df.format(new Date());
            // 此处是读写块数，成燃项目固定为2块*8位即可
            byte[] str2 = mNfcVUtil.readBlocks(startPoint, count);

            String info = NfcVUtil.bytesToHexString(str2).toUpperCase();
            nfcBean.setInfo(info);
            Log.e("nfc", "第" + startPoint + "块到第" + (startPoint + startPoint - 1) + "块数据为：");

            String endTime = df.format(new Date());

            String diff = getUseTime(startTime, endTime);
            nfcBean.setDiff(diff);
            Log.e("nfc", "用时:" + diff + "毫秒");
            return nfcBean;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        } finally {
            if (nfcVCard != null) {
                nfcVCard.debugPrint();
            }
        }
    }


    private static String getUseTime(String startTime, String endTime) {
        Calendar cStart = Calendar.getInstance();
        cStart.clear();
        cStart.set(Integer.parseInt(startTime.substring(0, 4)),
                Integer.parseInt(startTime.substring(4, 6)),
                Integer.parseInt(startTime.substring(6, 8)),
                Integer.parseInt(startTime.substring(8, 10)),
                Integer.parseInt(startTime.substring(10, 12)),
                Integer.parseInt(startTime.substring(12, 14)));
        Calendar cEnd = Calendar.getInstance();
        cEnd.clear();
        cEnd.set(Integer.parseInt(endTime.substring(0, 4)),
                Integer.parseInt(endTime.substring(4, 6)),
                Integer.parseInt(endTime.substring(6, 8)),
                Integer.parseInt(endTime.substring(8, 10)),
                Integer.parseInt(endTime.substring(10, 12)),
                Integer.parseInt(endTime.substring(12, 14)));
        long useTime = cEnd.getTimeInMillis() - cStart.getTimeInMillis();

        // 这里算出来的 是时间差 精确到毫秒 如果要精确到秒就自己再除1000
        return useTime + "";
    }
}
